package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.app.Dialog;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.SetTradePwdContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.SetTradePwdPresenter;

public class UserSetTradePwdAcivity extends BaseActivity<SetTradePwdContract.Presenter> implements SetTradePwdContract.View {

    private TextView mTitleTv;

    private EditText mPwdEt;

    private EditText mConfirmPwdEt;

    private EditText mPhoneEt;

    private TextView mSmsTv;

    private EditText mSmsEt;

    private TextView mNextTv;

    private ImageView mBackIm;

    @Override
    protected void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_trade_pwd;
    }

    @Override
    protected void initView() {
        mTitleTv = findViewById(R.id.set_pwd_title_tv);
        mPwdEt = findViewById(R.id.set_pwd_set_pwd_et);
        mPwdEt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
        mConfirmPwdEt = findViewById(R.id.set_pwd_confirm_pwd_et);
        mConfirmPwdEt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
        mPhoneEt = findViewById(R.id.set_pwd_phone_et);
        mSmsTv = findViewById(R.id.set_pwd_sms_tv);
        mSmsEt = findViewById(R.id.set_pwd_sms_et);
        mNextTv = findViewById(R.id.set_pwd_next_et);
        mBackIm = findViewById(R.id.set_pwd_back_im);
    }

    @Override
    protected void initData() {
        //mPresenter.isNextCanClick(mPwdEt,mConfirmPwdEt,mPhoneEt,mSmsEt);
        //mPresenter.isSmsCanClick(mPhoneEt);
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSmsTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(getPhone())){
                    showToast("请输入手机号码");
                    return;
                }
                mPresenter.getSmsCode(getPhone());
            }
        });
        mNextTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getConfirmPwd().equals(getPwd())){
                    if(TextUtils.isEmpty(getPhone())){
                        showToast("请输入手机号码");
                        return;
                    }
                    if(TextUtils.isEmpty(getSms())){
                        showToast("请输入验证码");
                        return;
                    }
                    if(TextUtils.isEmpty(getConfirmPwd())){
                        showToast("请输入密码");
                        return;
                    }
                    mPresenter.submit(getPhone(),getSms(),getPwd(),getConfirmPwd());
                }else{
                    showToast("两次输入密码不一致");
                }
            }
        });

    }

    @Override
    protected SetTradePwdContract.Presenter createPresenter() {
        return new SetTradePwdPresenter(this);
    }

    @Override
    public void setSmsCanClick(int textcolor, boolean enable) {
        mSmsTv.setTextColor(getResources().getColor(textcolor));
        mSmsTv.setEnabled(enable);
    }

    @Override
    public void setNextCanClick(int bgcolor, boolean enable) {
        mNextTv.setEnabled(enable);
        mNextTv.setBackgroundResource(bgcolor);
    }

    @Override
    public void setBackTime(String text, boolean ennable) {
        mSmsTv.setEnabled(ennable);
        mSmsTv.setText(text);
    }

    @Override
    public void setSendSmsSuccess(String msg) {
        showToast(msg);
    }

    @Override
    public void submitSuccess(String msg) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_trade_pwd_tip, null);
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(view);
        TextView tv = view.findViewById(R.id.dialog_confirm_tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
        dialog.show();
    }

    private String getPhone(){
        return mPhoneEt.getText().toString();
    }

    private String getSms(){
        return mSmsEt.getText().toString();
    }

    private String getPwd(){
        return mPwdEt.getText().toString();
    }

    private String getConfirmPwd(){
        return mConfirmPwdEt.getText().toString();
    }

    @Override
    public void setPresenter(SetTradePwdContract.Presenter presenter) {

    }
}
